package samfeng.erp.product.controller.admin;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import samfeng.erp.product.controller.admin.vo.*;
import samfeng.erp.product.convert.ProductConvert;
import samfeng.erp.product.dal.dataobject.ProductDO;
import samfeng.erp.product.service.ProductService;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;



@Api(tags = "管理后台 - 商品")
@RestController
@RequestMapping("/product/")
@Validated
public class ProductController {

    @Resource
    private ProductService productService;

    @PostMapping("/create")
    @ApiOperation("创建商品")
    @PreAuthorize("@ss.hasPermission('product:product:create')")
    public CommonResult<Long> create(@Valid @RequestBody ProductCreateReqVO createReqVO) {
        return success(productService.create(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新商品")
    @PreAuthorize("@ss.hasPermission('product:product:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody ProductUpdateReqVO updateReqVO) {
        productService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除商品")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:product:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        productService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得商品")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:product:query')")
    public CommonResult<ProductRespVO> get(@RequestParam("id") Long id) {
        ProductDO productDO = productService.get(id);
        return success(ProductConvert.INSTANCE.convert(productDO));
    }

    @GetMapping("/list")
    @ApiOperation("获得商品列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:product:query')")
    public CommonResult<List<ProductRespVO>> getList(@RequestParam("ids") Collection<Long> ids) {
        List<ProductDO> list = productService.getList(ids);
        return success(ProductConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得商品分页")
    @PreAuthorize("@ss.hasPermission('product:product:query')")
    public CommonResult<PageResult<ProductRespVO>> getPage(@Valid ProductPageReqVO pageVO) {
        PageResult<ProductDO> pageResult = productService.getPage(pageVO);
        return success(ProductConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出商品 Excel")
    @PreAuthorize("@ss.hasPermission('product:product:export')")
    @OperateLog(type = EXPORT)
    public void exportExcel(@Valid ProductExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProductDO> list = productService.getList(exportReqVO);
        // 导出 Excel
        List<ProductExcelVO> datas = ProductConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品.xls", "数据", ProductExcelVO.class, datas);
    }

    /**
     * 商品上下架操作
     * @param status
     * @param id
     * @return AjaxResult
     */
    @ApiOperation(value = "商品上下架操作")
    @PutMapping("/shelf")
    @PreAuthorize("@ss.hasPermission('product:product:update')")
    public CommonResult<Boolean> updateStatusById(@RequestParam(value = "status") String status,
                                                        @RequestParam(value = "id") String id){
        productService.updateProuctStatus(status, id);
        return success(true);
    }

}
